/**
 * @author bipul_sinha
 * @version 1.0
 * @created 30th November, 2010.
 *
 * AddToCart
 *
 * Copyright @ 2010
 * All Rights Reserved.
 * CONFIDENTIAL AND TRADE SECRET
 */
package com.ces.knacky.controller;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ces.knacky.framework.controller.ListController;
import com.ces.knacky.framework.model.IEntity;
/**
 * It is used to add items to cart. The items added are maintained in session and are displayed to 
 * the user.
 * 
 * @author bipul_sinha
 *
 */
@Controller
public class AddToCart extends ListController {
	/**
	 * Comma separated list of Item ids selected from the JSP page.
	 */
	private String selectedIds;	
	
	/**
	 * It initializes session object and add individual selected items into the session. Session
	 * maintains set of items added by a user.
	 *  
	 * @param 	request
	 * 			Request object to fetch session object. Session object is used to manage items 
	 * 			added by the user through multiple requests.  
	 * @return	String
	 * 			Represents View Resolver to be mapped in spring_views.xml file.
	 */
	@RequestMapping("/addToCart.htm")
	public String execute(HttpServletRequest request) {
		/*Set to store items selected by the user. */
		Set <String> cartSet = new HashSet <String>();
		if(selectedIds != null) {
			HttpSession session = request.getSession(true);
			Object cart = session.getAttribute("cart");
			/*If the cart is empty add the items directly. */
			if(cart == null){
				cartSet.addAll(Arrays.asList(selectedIds.split(",")));
				session.setAttribute("cart", cartSet);
			} else {
				/*If the cart is not empty merge the pre-existing items with the new ones.*/
				cartSet.addAll((Set<String>) cart);
				cartSet.addAll(Arrays.asList(selectedIds.split(",")));
				session.setAttribute("cart", cartSet);
			}			 
		}
		if(!cartSet.isEmpty()) {
			String cartString = cartSet.toString();
			String ids = cartString.substring(1, cartString.length()-1);
			listEntity = (List<IEntity>) commonService.list("SELECT item FROM Item item where item.id IN (" + ids + ")");		
		}
		return "addToCart";
	}

	/**
	 * This method is used to return comma separated list of Item ids selected by the user.
	 * 
	 * @return 	String
	 * 			Represents comma separated list of Item ids selected by the user.
	 */
	public String getSelectedIds() {
		return selectedIds;
	}

	/**
	 * It is used to set/map the ids selected by the user.
	 *  
	 * @param 	selectedIds
	 * 			Represents comma separated list of Item ids selected by the user.
	 */
	@ModelAttribute("selectedIds")
	public void setSelectedIds(String selectedIds) {
		this.selectedIds = selectedIds;
	}

}
